package com.qst.financing.security.filter;

import com.qst.financing.security.util.JwtTokenUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author QST
 * @Description 请求拦截器，根据传过来的token，进行认证
 * @Date 2021-08-24
 */
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    @Resource
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @SneakyThrows
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        // **新增：判断是否为公开路径，若是则直接放行**
        if (isPublicPath(request.getRequestURI())) {
            SecurityContextHolder.clearContext();
            chain.doFilter(request, response);
            return;
        }

        String token = extractToken(request);
        if (!StringUtils.isEmpty(token)) {
            String username = jwtTokenUtil.getUsernameFromToken(token);

            if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
                UserDetails userDetails = userDetailsService.loadUserByUsername(username);
                if (jwtTokenUtil.validateToken(token, userDetails)) {
                    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                    authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                    SecurityContextHolder.getContext().setAuthentication(authentication);
                }
            }
        }
        chain.doFilter(request, response);
    }

    // **新增：公开路径判断方法**
    private boolean isPublicPath(String uri) {
        // 定义需要放行的公开路径（与 WebSecurityConfig 中的 permitAll 路径一致）
        List<String> publicPaths = Arrays.asList(
                "/file/**",
                "/swagger-ui.html#/**",
                "/swagger-ui.html/**",
                "/user/add/**"
        );
        return publicPaths.stream().anyMatch(uri::startsWith);
    }

    private String extractToken(HttpServletRequest request) {
        // 首先从请求头中获取token
        String token = request.getHeader(jwtTokenUtil.getHeader());
        
        // 如果请求头中没有token，尝试从请求参数中获取
        if (StringUtils.isEmpty(token)) {
            token = request.getParameter("token");
        }
        
        // 如果请求参数中没有token，尝试从请求体中获取
        if (StringUtils.isEmpty(token)) {
            token = request.getParameter("Authorization");
        }
        
        // 如果token以"Bearer "开头，去掉这个前缀
        if (!StringUtils.isEmpty(token) && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        
        return token;
    }
}
